package com.pai4j.common.enums.community;
import com.pai4j.common.exception.UnValidResourceException;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;


/***
 * 资源格式
 */
@Getter
public enum ResourceFormatEnum {

    IMAGE, PDF, DOC, EXCEL, PPT, ZIP, EXE, DMG;

    private static final List<String> IMAGE_EXT_SUFFIX = Arrays.asList("jpg", "png", "gif", "jpeg");

    private static final List<String> PDF_EXT_SUFFIX = Arrays.asList("pdf");

    private static final List<String> DOC_EXT_SUFFIX = Arrays.asList("doc", "docx");

    private static final List<String> EXCEL_EXT_SUFFIX = Arrays.asList("xls", "xlsx", "csv");

    private static final List<String> PPT_EXT_SUFFIX = Arrays.asList("ppt", "pptx", "pps");

    private static final List<String> ZIP_EXT_SUFFIX = Arrays.asList("rar", "zip", "jar", "aex", "tar", "gz");

    private static final List<String> EXE_EXT_SUFFIX = Arrays.asList("exe");

    private static final List<String> DMG_EXT_SUFFIX = Arrays.asList("dmg");

    public static List<String> getExtNameListByPath(String path) {
        if (StringUtils.isBlank(path)) {
            return new ArrayList<>();
        }
        if (path.equals(ResourcePathEnum.PDF.name())) {
            return PDF_EXT_SUFFIX;
        }
        if (path.equals(ResourcePathEnum.OFFICE.name())) {
            List<String> officeExtNameList = new ArrayList<>();
            officeExtNameList.addAll(DOC_EXT_SUFFIX);
            officeExtNameList.addAll(EXCEL_EXT_SUFFIX);
            officeExtNameList.addAll(PPT_EXT_SUFFIX);
            return officeExtNameList;
        }
        if (path.equals(ResourcePathEnum.ZIP.name())) {
            return ZIP_EXT_SUFFIX;
        }
        if (path.equals(ResourcePathEnum.WIN_SOFTWARE.name())) {
            return EXE_EXT_SUFFIX;
        }
        if (path.equals(ResourcePathEnum.MAC_SOFTWARE.name())) {
            return DMG_EXT_SUFFIX;
        }
        return new ArrayList<>();
    }

    /****
     * 获取所有支持的文件格式
     * @return
     */
    public static List<String> getSupportResourceFormat() {
        List<String> formatList = new ArrayList<>();
        for (ResourceFormatEnum format : ResourceFormatEnum.values()) {
            formatList.add(format.name());
        }
        return formatList;
    }

    /***
     * 获取文件扩展名
     * @param fileName
     * @return
     */
    public static String getFileExtNameByFileName(String fileName) {
        if (StringUtils.isBlank(fileName) || fileName.indexOf(".") == -1) {
            return null;
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /***
     * 获取文件大的格式分类
     * @param fileUrl
     * @return
     */
    public static String getResourceFormatByFileName(String fileUrl) {
        return getResourceFormatByExtName(getFileExtNameByFileName(fileUrl));
    }

    public static String getResourceFormatByExtName(String extName) {
        if (StringUtils.isBlank(extName)) {
            throw new UnValidResourceException("不支持的文件格式");
        }
        String lowerExtName = extName.toLowerCase(Locale.ROOT);
        String upperExtName = extName.toUpperCase(Locale.ROOT);
        if (PDF_EXT_SUFFIX.contains(lowerExtName) || PDF_EXT_SUFFIX.contains(upperExtName)) {
            return PDF.name();
        }
        if (DOC_EXT_SUFFIX.contains(lowerExtName) || DOC_EXT_SUFFIX.contains(upperExtName)) {
            return DOC.name();
        }
        if (EXCEL_EXT_SUFFIX.contains(lowerExtName) || EXCEL_EXT_SUFFIX.contains(upperExtName)) {
            return EXCEL.name();
        }
        if (PPT_EXT_SUFFIX.contains(lowerExtName) || PPT_EXT_SUFFIX.contains(upperExtName)) {
            return PPT.name();
        }
        if (ZIP_EXT_SUFFIX.contains(lowerExtName) || ZIP_EXT_SUFFIX.contains(upperExtName)) {
            return ZIP.name();
        }
        if (EXE_EXT_SUFFIX.contains(lowerExtName) || EXE_EXT_SUFFIX.contains(upperExtName)) {
            return EXE.name();
        }
        if (DMG_EXT_SUFFIX.contains(lowerExtName) || DMG_EXT_SUFFIX.contains(upperExtName)) {
            return DMG.name();
        }
        if (IMAGE_EXT_SUFFIX.contains(lowerExtName) || IMAGE_EXT_SUFFIX.contains(upperExtName)) {
            return IMAGE.name();
        }
        throw new UnValidResourceException("不支持的文件格式");
    }

    public static void validPathExcludeExtName(String path, String extName) {
        if (path.equals(ResourcePathEnum.CLOUD.name())) {
            return;
        }
        boolean contains = getExtNameListByPath(path).contains(extName);
        if (contains) {
            return;
        }
        throw new UnValidResourceException(ResourcePathEnum.valueOf(path).getPath()+"下不支持"+extName+"格式的文件，请切换相关分类");
    }
}
